/*
 * LcdWidget.cpp
 *
 *  Created on: 2012-11-27
 *      Author: kylin
 */

#include <QDateTime>
#include <QString>
#include <QTimer>
#include <QtCore/qmath.h>

#include "LcdWidget.h"
#include "DprLockManager.h"
#include "DprLcd.h"

//////////////////////////////////////////////////////////////////////
// class LcdWidget
LcdWidget::LcdWidget(int id,const QString &title,
		DprLockManager* lockManager,DprLcd *lcd,LcdWidget *parent/*=0*/)
 : m_id(id),m_title(title),m_lockManager(lockManager),
   m_lcd(lcd),m_parent(parent)
{
	
}

LcdWidget::~LcdWidget()
{

}

//////////////////////////////////////////////////////////////////////
// class LcdWidgetMain
LcdWidgetMain::LcdWidgetMain(int id,const QString &title,
		DprLockManager* lockManager,DprLcd *lcd,LcdWidget *parent/*=0*/)
 : LcdWidget(id,title,lockManager,lcd,parent)
{
	m_timerId=0;
}

LcdWidgetMain::~LcdWidgetMain()
{
	if(m_timerId)
	{
		killTimer(m_timerId);
		m_timerId=0;
	}
}

void LcdWidgetMain::reset()
{
	if(m_timerId)
	{
		killTimer(m_timerId);
		m_timerId=0;
	}
}

void LcdWidgetMain::keyEvent(KeyEvent &event)
{

}

void LcdWidgetMain::update()
{

}

void LcdWidgetMain::show()
{
	draw();
	if(m_timerId)
		return;

	m_timerId=startTimer(3000);
}

void LcdWidgetMain::timerEvent(QTimerEvent *)
{
	draw();
}

void LcdWidgetMain::draw()
{
	m_lcd->clearScreen();
	QString strTime=QDateTime::currentDateTime().
			toString(tr("yyyy/MM/dd hh:mm:ss"));
	m_lcd->drawText(0,strTime,LcdAlignmentCenter,false);

	m_lcd->drawText(1,tr("三开一防逻辑服务器"),LcdAlignmentCenter,false);

}

//////////////////////////////////////////////////////////////////////
// class LcdWidgetMenu
LcdWidgetMenu::LcdWidgetMenu(int id,const QString &title,
		DprLockManager* lockManager,DprLcd *lcd,LcdWidget *parent/*=0*/)
 : LcdWidget(id,title,lockManager,lcd,parent)
{
	m_curMenu=0;
}

LcdWidgetMenu::~LcdWidgetMenu()
{

}

void LcdWidgetMenu::reset()
{

}

void LcdWidgetMenu::keyEvent(KeyEvent &event)
{
	int lineCount=qCeil(m_lstMenuText.size()/2);
	int preMenu=m_curMenu;
	switch(event.keyCode)
	{
		case keyUp:
			if(!m_curMenu)
				m_curMenu=m_lstMenuText.size()-1;
			else
				m_curMenu=(m_curMenu-1)%6;
			drawMenuItem(preMenu);
			drawMenuItem(m_curMenu);
			break;

		case keyDown:
			if(m_curMenu==(m_lstMenuText.size()-1))
				m_curMenu=0;
			else
				m_curMenu=(m_curMenu+1)%6;
			drawMenuItem(preMenu);
			drawMenuItem(m_curMenu);
			break;

		case keyLeft:
			if(m_curMenu<lineCount)
				break;
			m_curMenu-=lineCount;
			drawMenuItem(preMenu);
			drawMenuItem(m_curMenu);
			break;

		case keyRight:
			if(m_curMenu>=lineCount)
				break;
			m_curMenu+=lineCount;
			drawMenuItem(preMenu);
			m_curMenu=(m_curMenu>=m_lstMenuText.size())?
					(m_lstMenuText.size()-1):m_curMenu;
			drawMenuItem(m_curMenu);
			break;
	}
}

void LcdWidgetMenu::update()
{

}

void LcdWidgetMenu::show()
{
	m_curMenu=0;
	draw();
}

void LcdWidgetMenu::draw()
{
	m_lcd->clearScreen();

	m_lcd->drawText(0,m_title,LcdAlignmentCenter,false);

	int line=1;
	int indent=1;
	bool revert=false;
	int lineCount=qCeil(m_lstMenuText.size()/2);
	for(int i=0;i<m_lstMenuText.size();i++)
	{
		if(i==lineCount)
		{
			line=1;
			indent=7;
		}

		if(m_curMenu==i)
		{
			m_lcd->drawSymbol(line,indent-1,lcdFinger,false);
			revert=true;
		}

		m_lcd->drawText(line,indent,m_lstMenuText.at(i),revert);
		line++;
		revert=false;
	}
}

void LcdWidgetMenu::drawMenuItem(int index)
{
	int line,indent;
	bool revert=false;

	int lineCount=qCeil(m_lstMenuText.size()/2);

	if(index>=lineCount)
	{
		indent=7;
		line=1+index-lineCount;
	}
	else
	{
		indent=1;
		line=index+1;
	}

	if(m_curMenu==index)
	{
		revert=true;
		m_lcd->drawSymbol(line,indent-1,lcdFinger,false);
	}
	else
	{
		revert=false;
		m_lcd->drawSymbol(line,indent-1,lcdBlank,false);
	}

	m_lcd->drawText(line,indent,m_lstMenuText.at(index),revert);
}

//////////////////////////////////////////////////////////////////////
// class LcdWidgetEvent
LcdWidgetEvent::LcdWidgetEvent(int id,const QString &title,
		DprLockManager* lockManager,DprLcd *lcd,LcdWidget *parent/*=0*/)
 : LcdWidget(id,title,lockManager,lcd,parent)
{

}

LcdWidgetEvent::~LcdWidgetEvent()
{

}

void LcdWidgetEvent::reset()
{

}

void LcdWidgetEvent::keyEvent(KeyEvent &event)
{

}

void LcdWidgetEvent::update()
{

}

void LcdWidgetEvent::show()
{

}

void LcdWidgetEvent::draw()
{

}

//////////////////////////////////////////////////////////////////////
// class LcdWidgetTable
LcdWidgetTable::LcdWidgetTable(int id,const QString &title,
		DprLockManager* lockManager,DprLcd *lcd,LcdWidget *parent/*=0*/)
 : LcdWidget(id,title,lockManager,lcd,parent)
{

}

LcdWidgetTable::~LcdWidgetTable()
{

}

void LcdWidgetTable::reset()
{

}

void LcdWidgetTable::keyEvent(KeyEvent &event)
{

}

void LcdWidgetTable::update()
{

}

void LcdWidgetTable::show()
{

}

void LcdWidgetTable::draw()
{

}

//////////////////////////////////////////////////////////////////////
// class LcdManager
#define LCD_ON_TIME 60000
DprWidgetManager::DprWidgetManager(DprLockManager *lockManager,DprLcd *lcd,
		QObject *parent/*=0*/)
 : m_lockManager(lockManager),m_lcd(lcd)
{
	LcdWidget *widgetTemp,*parentWidget;
	int curId=0;

	curId=0xFF;
	widgetTemp=new LcdWidgetMain(curId,"",lockManager,lcd);
	m_widgets.insert(curId,widgetTemp);
	m_widgetMain=curId;

	parentWidget=widgetTemp;

	curId=0xFE;
	LcdWidgetMenu *widgetMenu=new LcdWidgetMenu(curId,tr("【主 菜 单】"),lockManager,lcd,
			parentWidget);
	widgetMenu->setMenuList(QStringList()<<tr("开 入 量")<<tr("定值操作")
			<<tr("装置参数")<<tr("历史报告")
			<<tr("装置状态")<<tr("其他功能"));
	m_widgets.insert(curId,widgetMenu);
	m_widgetMenu=curId;

	m_widgets.value(m_widgetMain)->show();
	m_currentWidget=m_widgetMain;

	m_timerLcd=new QTimer;
	m_timerLcd->setSingleShot(true);
	connect(m_timerLcd,SIGNAL(timeout()),this,SLOT(lcdTimer()));

	m_timerLcd->start(LCD_ON_TIME);
}

DprWidgetManager::~DprWidgetManager()
{

}

void DprWidgetManager::keyEvent(int code)
{
	m_timerLcd->stop();
	m_lcd->setBackLight(true);
	m_timerLcd->start(LCD_ON_TIME);
	KeyEvent event;
	event.keyCode=code;
	LcdWidget* curWidget=m_widgets.value(m_currentWidget);
	LcdWidget* parent=curWidget->parent();

	switch(code)
	{
		case keyEnter:
		{
			if(m_currentWidget==m_widgetMain)
			{
				m_widgets.value(m_currentWidget)->reset();
				m_currentWidget=m_widgetMenu;
				m_widgets.value(m_currentWidget)->show();
			}

			break;
		}

		case keyUp:
		case keyLeft:
		case keyDown:
		case keyRight:
		{
			m_widgets.value(m_currentWidget)->keyEvent(event);
			break;
		}

		case keyCancel:
		{
			if(!parent)
				break;
			curWidget->reset();
			parent->show();
			m_currentWidget=m_widgets.key(parent);
			break;
		}
	}
}

void DprWidgetManager::lcdTimer()
{
	m_lcd->setBackLight(false);
}
